#include "bsp.h"

unsigned short arrLCDClearArea[20][4] = {
	{5, 5, 230, 50}, 			// 标题
	{5, 60, 230, 60 + 30},	 	// 1
	{5, 95, 230, 95 + 30},	 	// 2
	{5, 130, 230, 130 + 30}, 	// 3
	{5, 165, 230, 165 + 26}, 	// 4
//	{5, 206, 230, 205 + 25}, 	// 激光编码伪随机
	{105, 206, 230, 205 + 25}, 	// 激光编码伪随机
	{5, 230, 230, 230 + 20}, 	// 激光状态
	{5, 255, 230, 255 + 20}, 	// 电源状态
	{5, 280, 200, 280 + 40}, 	// 激光时间
	{85, 206, 230, 205 + 30}, 	// 伪随机
	{85, 230, 230, 230 + 20}, 	// 激光状态
	{85, 255, 230, 255 + 20}, 	// 电源状态	
	{170, 280, 230, 280 + 40}, 	// 时间
	{30, 60, 230, 90}, 			// 伪随机数据清楚
	{10, 40, 260, 185}, 		// 二级菜单，精确频率编码下面的编码值和数据
	{80, 40, 260, 185}, 		// 二级菜单，精确频率编码下面的数据
	{5 , 5 , 230 , 200}, 			// 上界面全部刷新
	{5 , 5 , 230 , 320}, 		//
	
};


/// @brief 周期數值減少
/// @param getNLaserCodDig 
/// @param getNTempLaser 
/// @return 
unsigned int setCodingReduction(unsigned int getNLaserCodDig , unsigned int getNTempLaser)
{
	unsigned int nreturn = 0;
	unsigned int nLaserCodDig = getNLaserCodDig;
	unsigned int nTempLaserCod = getNTempLaser;
	//unsigned int nMaxLaserCod = CYCLELIMITMAX;
	unsigned int nMinLaserCod = CYCLELIMITMIN;	
	switch(nLaserCodDig)
	{									
	// case 1://百位  50 000 //? 周期暂时不需要百位
	// if(nTempLaserCod > 100000)
	// 	nTempLaserCod = nTempLaserCod - 100000;
	// else if(nTempLaserCod <= CYCLELIMITMIN)
	// 	nTempLaserCod = CYCLELIMITMIN;
	// break;

	case 2://十位
	//										if(nTempLaserCod > 9999)
	if(nTempLaserCod > nMinLaserCod)
	nTempLaserCod = nTempLaserCod - 10000;
	else if(nTempLaserCod <= nMinLaserCod)
	nTempLaserCod = nMinLaserCod;
	break;

	case 3://个位
	//										if(nTempLaserCod > 999)
	if(nTempLaserCod > nMinLaserCod)
	nTempLaserCod = nTempLaserCod - 1000;
	else if(nTempLaserCod <= nMinLaserCod)
	nTempLaserCod = nMinLaserCod;
	break;

	case 4://小数点后一位
	//										if(nTempLaserCod > 99)
	if(nTempLaserCod > nMinLaserCod)
	nTempLaserCod = nTempLaserCod - 100;
	else if(nTempLaserCod <= nMinLaserCod)
	nTempLaserCod = nMinLaserCod;
	break;

	case 5://小数点后二位
	//if(nTempLaserCod > 9)
	if(nTempLaserCod > nMinLaserCod)
	nTempLaserCod = nTempLaserCod - 10;
	else if(nTempLaserCod <= nMinLaserCod)
	nTempLaserCod = nMinLaserCod;
	break;

	case 6://小数点后三位
	//if(nTempLaserCod > 1)
	if(nTempLaserCod > nMinLaserCod)
	nTempLaserCod = nTempLaserCod - 1;
	else if(nTempLaserCod <= nMinLaserCod)
	nTempLaserCod = nMinLaserCod;
	break;									
	}		
		nreturn = nTempLaserCod;
	return nreturn;
}


/// @brief 周期數字增加
/// @param getNLaserCodDig 
/// @param getNTempLaser 
/// @return 
unsigned int setCodingIncrease(unsigned int getNLaserCodDig , unsigned int getNTempLaser)
{
	unsigned int nLaserCodDig = getNLaserCodDig;
	unsigned int nTempLaserCod = getNTempLaser;
	unsigned int nMaxLaserCod = CYCLELIMITMAX;
	//unsigned int nMinLaserCod = CYCLELIMITMIN;
	unsigned int nreturn = 0;
	switch(nLaserCodDig)
	{									
	// case 1://百位  50 000 //? 周期暂时不需要百位
	// if(nTempLaserCod < 100000)
	// 	nTempLaserCod = nTempLaserCod + 100000;
	// else if(nTempLaserCod >= 120000)
	// 	nTempLaserCod = nMinLaserCod;
	// break;

	case 2://十位
	if(nTempLaserCod < nMaxLaserCod)
	nTempLaserCod = nTempLaserCod + 10000;
	else if(nTempLaserCod >= nMaxLaserCod)
	nTempLaserCod = nMaxLaserCod;
	break;

	case 3://个位
	if(nTempLaserCod < nMaxLaserCod)
	nTempLaserCod = nTempLaserCod + 1000;
	else if(nTempLaserCod >= nMaxLaserCod)
	nTempLaserCod = nMaxLaserCod;
	break;

	case 4://小数点后一位
	if(nTempLaserCod < nMaxLaserCod)
	nTempLaserCod = nTempLaserCod + 100;
	else if(nTempLaserCod >= nMaxLaserCod)
	nTempLaserCod = nMaxLaserCod;
	break;

	case 5://小数点后二位
	if(nTempLaserCod < nMaxLaserCod)
	nTempLaserCod = nTempLaserCod + 10;
	else if(nTempLaserCod >= nMaxLaserCod)
	nTempLaserCod = nMaxLaserCod;
	break;

	case 6://小数点后三位
	if(nTempLaserCod < nMaxLaserCod)
	nTempLaserCod = nTempLaserCod + 1;
	else if(nTempLaserCod >= nMaxLaserCod)
	nTempLaserCod = nMaxLaserCod;
	break;									
	}	
	nreturn = nTempLaserCod;
	return nreturn;
}

/// @brief 频率数据显示 , 当暂时没用这个函数
/// @param nLoop 
void DISPlay_Function_LaserLoop(unsigned short nLoop)
{
//	unsigned char 	nxdata = 90;
	unsigned char 	nxdata = 90 + 80;
	unsigned short 	nydata =  210 + 75;
	unsigned char 	nfontsize = 24;
	unsigned short  nGetNum = nLoop;
	int bits = getDecimalDigits(nGetNum);
	LCD_ClearArea(arrLCDClearArea[12][0] , arrLCDClearArea[12][1] , arrLCDClearArea[12][2] , arrLCDClearArea[12][3] , DARKBLUE);		
	LCD_ShowIntNum(nxdata , nydata , nGetNum , bits , WHITE ,DARKBLUE , nfontsize);	
}

/// @brief 精确频率编码显示
/// @param nCoding 
/// @return 
unsigned char funFrequencyNumberSet(unsigned int nCoding)
{
	DigitInfo result = extractDigits(nCoding);
	unsigned char nre = 0;
	unsigned char nxdata = 100;
	unsigned short nydata =  98;
	unsigned char nfontsize = 32;
	unsigned char noffset = 0;	
	unsigned char nCodDig = result.count  - 3;//整数位数
	if(nCodDig == 0)
	{
		return 0;
	}
	switch(nCodDig)
	{
		case 1:	//个
		LCD_ShowIntNum(nxdata , nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		noffset = 20;
		LCD_ShowIntNum(nxdata + noffset , nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		noffset = 40;
		LCD_ShowIntNum(nxdata + noffset , nydata , result.digits[result.count - 1] , 1 , WHITE , BLUE , nfontsize);
		/**以下为小数部分**/
		nxdata = 160;
		LCD_ShowChar_Size(nxdata , nydata , '.' , WHITE , BLUE , nfontsize , 1);
		nxdata = 170;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 2] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 20;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 3] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 40;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , BLUE , nfontsize);	
		break;
		
		case 2:	//十
		LCD_ShowIntNum(nxdata , nydata , 0 , 1 , WHITE , BLUE , nfontsize);
		noffset = 20;
		LCD_ShowIntNum(nxdata + noffset , nydata , result.digits[result.count - 1] , 1 , WHITE , BLUE , nfontsize);
		noffset = 40;
		LCD_ShowIntNum(nxdata + noffset , nydata , result.digits[result.count - 2] , 1 , WHITE , BLUE , nfontsize);
		/**以下为小数部分**/		
		nxdata = 160;
		LCD_ShowChar_Size(nxdata , nydata , '.' , WHITE , BLUE , nfontsize , 1);
		nxdata = 170;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 3] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 20;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 40;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 5] , 1 , WHITE , BLUE , nfontsize);		
		break;
		
		case 3:
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 1] , 1 , WHITE , BLUE , nfontsize);
		noffset = 20;
		LCD_ShowIntNum(nxdata + 20 , nydata , result.digits[result.count - 2] , 1 , WHITE , BLUE , nfontsize);
		noffset = 40;
		LCD_ShowIntNum(nxdata + 40 , nydata , result.digits[result.count - 3] , 1 , WHITE , BLUE , nfontsize);
		/**以下为小数部分**/
		nxdata = 160;
		LCD_ShowChar_Size(nxdata , nydata , '.' , WHITE , BLUE , nfontsize , 1);
		nxdata = 170;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 4] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 20;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 5] , 1 , WHITE , BLUE , nfontsize);
		nxdata = 170 + 40;
		LCD_ShowIntNum(nxdata , nydata , result.digits[result.count - 6] , 1 , WHITE , BLUE , nfontsize);
		break;
	}
	/**以下为小数部分**/
	return nre;
}

/// @brief 精确频率码 数值选择 , 也就是精确编码数值的框框
/// @param nNumberDig 
/// @param nset 
/// @param nfsetbit 
void funFrequencySonMode(unsigned char nNumberDig ,unsigned int nset)
{
//	unsigned char nGetNumDig = nNumberDig;
	unsigned short 	nstxdata = 5;
	unsigned short 	nstydata =  5;
	
	unsigned short 	nendxdata = 5;
	unsigned short 	nendydata =  5;
	
	unsigned char nxdata = 5;
	unsigned short nydata =  5;	
	//unsigned char nfontsize = 32;
	//unsigned char noffset = 96;	
	
	nstydata  = 100;
	nendydata = 130;
	
	unsigned int ntest = nset;	
	//unsigned char nGetfset = nfsetbit;
	switch(nNumberDig)
	{
	case 1: //80, 40 , 260 , 185
	nstxdata  = 100;
	nendxdata = 115;
	funFrequencyNumberSet(ntest);
	setFrequencyEncod(nNumberDig);
	break;
	
	case 2: //? 十位
	nstxdata  = 100 + 35/2;
	nendxdata = 115 + 35/2;
	funFrequencyNumberSet(ntest);
	setFrequencyEncod(nNumberDig);
	break;
	
	case 3: //? 个位
	nstxdata  = 100 + 35 * 1;
	nendxdata = 115 + 35 * 1;
	funFrequencyNumberSet(ntest);
	setFrequencyEncod(nNumberDig);
	break;
	
	case 4: //? 小数点 1
	nstxdata  = 100 + 35 * 1 + 30;
	nendxdata = 115 + 35 * 1 + 30;
	funFrequencyNumberSet(ntest);
	setFrequencyEncod(nNumberDig);
	break;
	
	case 5: //? 小数点2
	nstxdata  = 100 + 35 * 1 + 45;
	nendxdata = 115 + 35 * 1 + 45;
	funFrequencyNumberSet(ntest);
	setFrequencyEncod(nNumberDig);
	break;
	
	case 6: //? 小数点3
	nstxdata  = 100 + 35 * 1 + 30 * 2;
	nendxdata = 115 + 35 * 1 + 30 * 2;
	funFrequencyNumberSet(ntest);	
	setFrequencyEncod(nNumberDig);
	break;	
	
	case 7:
	funFrequencyNumberSet(ntest);
	setFrequencyEncod(0);
	break;
	}	
}


/// @brief 精确频率编码框框选择 nendxdata = 左边竖直长度 nstydata = 最上边的长度 nendydata = 右边垂直长度
/// @param digit 
void setFrequencyEncod(unsigned char digit)
{
	unsigned short 	nstxdata[6]  = {117,  117 + 20*1 , 117 + 20*2, 117 + 30 + 20*2 , 117  + 30 + 20*3 , 117+ 30 + 20*4};
	unsigned short 	nstydata[6]  = {98 ,  98 + 20*1 , 98 + 20*2 , 98 + 29 + 20*2 , 98+ 29 + 20*3 , 98+ 29 + 20*4};
	
	unsigned short 	nendxdata[6] = {95 , 95 , 95 , 95, 95 , 95};
	unsigned short 	nendydata[6] = {130, 130 , 130 , 130, 130 , 130};
	
	unsigned char nGetDig = digit;
	switch(nGetDig)
	{
    case 0:
    LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0], nendydata[0] , BLUE);	
    LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1], nendydata[1] , BLUE);	
    LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2], nendydata[2] , BLUE);	
    LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3], nendydata[3] , BLUE);	
    LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4], nendydata[4] , BLUE);	
    LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5], nendydata[5] , BLUE);	
    break;

	case 1:
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0], nendydata[0] , WHITE);	
	break;
	
	case 2:
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1], nendydata[1] , WHITE);	
	break;
	
	case 3:
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2], nendydata[2] , WHITE);		
	break;
	
	case 4:
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);	
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3], nendydata[3] , WHITE);	
	break;
	
	case 5:
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5] , nendydata[5] , BLUE);
	
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4], nendydata[4] , WHITE);	
	break;
	
	case 6:
	LCD_DrawRectangle(nstxdata[1], nendxdata[1] , nstydata[1] , nendydata[1] , BLUE);
	LCD_DrawRectangle(nstxdata[2], nendxdata[2] , nstydata[2] , nendydata[2] , BLUE);
	LCD_DrawRectangle(nstxdata[3], nendxdata[3] , nstydata[3] , nendydata[3] , BLUE);
	LCD_DrawRectangle(nstxdata[4], nendxdata[4] , nstydata[4] , nendydata[4] , BLUE);
	LCD_DrawRectangle(nstxdata[0], nendxdata[0] , nstydata[0] , nendydata[0] , BLUE);
	
	LCD_DrawRectangle(nstxdata[5], nendxdata[5] , nstydata[5], nendydata[5] , WHITE);	
	break;		
	}
}

